import { createRouter, createWebHashHistory, RouteRecordRaw } from 'vue-router';
import { usePermissStore } from '../store/permiss';
import Home from '../views/home.vue';
// 显示 页面 的栏目 写下面--标签栏的名字对应-----------这里是权限，路由守卫
const routes: RouteRecordRaw[] = [
    {
        path: '/',//当url写空时，跳转user表
        redirect: '/dedearchives',
    },
    {
        path: '/',
        name: 'Home',
        component: Home,
        children: [
            {
                path: '/dedeadmin',
                name: 'dedeadmin',
                meta: {
                    title: 'dede管理员表格',
                    permiss: '1',
                },
                component: () => import(/* webpackChunkName: "table" */ '../views/dede/dedeadmin/dedeadmin.vue'),
            },
            {
                path: '/dedearchives',
                name: 'dedearchives',
                meta: {
                    title: 'dede文章',
                    permiss: '1',
                },
                component: () => import(/* webpackChunkName: "table" */ '../views/dede/dedearchives/dedearchives.vue'),
            },

            {
                path: '/dashboard',
                name: 'dashboard',
                meta: {
                    title: '主控制台',
                    permiss: '1',
                },
                component: () => import(/* webpackChunkName: "dashboard" */ '../views/dashboard.vue'),
            },

            {
                path: '/table',
                name: 'basetable',
                meta: {
                    title: 'admin管理员表格',
                    permiss: '3',
                },
                component: () => import(/* webpackChunkName: "table" */ '../views/table.vue'),
            },
            {
                path: '/adminGroup',
                name: 'adminGroup',
                meta: {
                    title: '管理员角色组',
                    permiss: '4',
                },
                component: () => import(/* webpackChunkName: "table" */ '../views/adminGroup.vue'),
            },


            {
                path: '/rjlb/software',
                name: 'rjlbsoftware',
                meta: {
                    title: '软件列表',
                    permiss: '5',
                },
                component: () => import(/* webpackChunkName: "table" */ '../views/rjlb/software.vue'),
            },
            {
                path: '/user',
                name: 'basexinuser',
                meta: {
                    title: '用户列表',
                    permiss: '7',
                },
                component: () => import(/* webpackChunkName: "table" */ '../views/user.vue'),
            },
            {
                path: '/syjl/logtry',
                name: 'logtry',
                meta: {
                    title: '试用记录',
                    permiss: '9',
                },
                component: () => import(/* webpackChunkName: "table" */ '../views/syjl/logtry.vue'),
            },
                        {
                path: '/zxlb/logonline',
                name: 'logonline',
                meta: {
                    title: '在线列表',
                    permiss: '10',
                },
                component: () => import(/* webpackChunkName: "table" */ '../views/zxlb/logonline.vue'),
            },
            {
                path: '/kzlb/card',
                name: 'card',
                meta: {
                    title: '卡种列表',
                    permiss: '11',
                },
                component: () => import(/* webpackChunkName: "table" */ '../views/kzlb/card.vue'),
            },
            {
                path: '/czkb/cardrefill',
                name: 'cardrefill',
                meta: {
                    title: '充值卡表',
                    permiss: '12',
                },
                component: () => import(/* webpackChunkName: "table" */ '../views/czkb/cardrefill.vue'),
            },
            {
                path: '/czjl/logcardrefill',
                name: 'logcardrefill',
                meta: {
                    title: '充值卡记录',
                    permiss: '13',
                },
                component: () => import(/* webpackChunkName: "table" */ '../views/czjl/logcardrefill.vue'),
            },
            {
                path: '/hmdb/blacklist',
                name: 'blacklist',
                meta: {
                    title: '黑名单表',
                    permiss: '14',
                },
                component: () => import(/* webpackChunkName: "table" */ '../views/hmdb/blacklist.vue'),
            },
            {
                path: '/fhqb/firewall',
                name: 'firewall',
                meta: {
                    title: '防火墙表',
                    permiss: '15',
                },
                component: () => import(/* webpackChunkName: "table" */ '../views/fhqb/firewall.vue'),
            },
            {
                path: '/plcz/batch',
                name: 'batch',
                meta: {
                    title: '批量操作',
                    permiss: '16',
                },
                component: () => import(/* webpackChunkName: "table" */ '../views/plcz/batch.vue'),
            },
            {
                path: '/plqk/take',
                name: 'take',
                meta: {
                    title: '批量取卡',
                    permiss: '17',
                },
                component: () => import(/* webpackChunkName: "table" */ '../views/plqk/take.vue'),
            },
            {
                path: '/xlcx/sales',
                name: 'sales',
                meta: {
                    title: '销量查询',
                    permiss: '18',
                },
                component: () => import(/* webpackChunkName: "table" */ '../views/xlcx/sales.vue'),
            },
     
            {
                path: '/yxgl/email',
                name: 'email',
                meta: {
                    title: '邮箱管理',
                    permiss: '20',
                },
                component: () => import(/* webpackChunkName: "table" */ '../views/yxgl/email.vue'),
            },
            {
                path: '/dxgl/sms',
                name: 'sms',
                meta: {
                    title: '短信管理',
                    permiss: '21',
                },
                component: () => import(/* webpackChunkName: "table" */ '../views/dxgl/sms.vue'),
            },
            {
                path: '/zfgl/pay',
                name: 'pay',
                meta: {
                    title: '支付管理',
                    permiss: '22',
                },
                component: () => import(/* webpackChunkName: "table" */ '../views/zfgl/pay.vue'),
            },
            {
                path: '/goods',
                name: 'basegoods',
                meta: {
                    title: '充值套餐',
                    permiss: '2',
                },
                component: () => import(/* webpackChunkName: "table" */ '../views/goods.vue'),
            },
            {
                path: '/order',
                name: 'baseorder',
                meta: {
                    title: '订单列表',
                    permiss: '2',
                },
                component: () => import(/* webpackChunkName: "table" */ '../views/order.vue'),
            },

            {
                path: '/userwx',
                name: 'basexinuserwx',
                meta: {
                    title: '微信会员',
                    permiss: '2',
                },
                component: () => import(/* webpackChunkName: "table" */ '../views/userwx.vue'),
            },
            {
                path: '/htconfig',
                name: 'basexihtconfig',
                meta: {
                    title: '微信支付配置',
                    permiss: '2',
                },
                component: () => import(/* webpackChunkName: "table" */ '../views/htconfig.vue'),
            },
            {
                path: '/htconfigzfb',
                name: 'basexihtconfigzfb',
                meta: {
                    title: '支付宝配置',
                    permiss: '2',
                },
                component: () => import(/* webpackChunkName: "table" */ '../views/htconfigzfb.vue'),
            },
           
            {
                path: '/charts',
                name: 'basecharts',
                meta: {
                    title: '图表',
                    permiss: '11',
                },
                component: () => import(/* webpackChunkName: "charts" */ '../views/charts.vue'),
            },
            {
                path: '/form',
                name: 'baseform',
                meta: {
                    title: '表单',
                    permiss: '5',
                },
                component: () => import(/* webpackChunkName: "form" */ '../views/form.vue'),
            },
            {
                path: '/tabs',
                name: 'tabs',
                meta: {
                    title: 'tab标签',
                    permiss: '3',
                },
                component: () => import(/* webpackChunkName: "tabs" */ '../views/tabs.vue'),
            },
            {
                path: '/donate',
                name: 'donate',
                meta: {
                    title: '鼓励作者',
                    permiss: '14',
                },
                component: () => import(/* webpackChunkName: "donate" */ '../views/donate.vue'),
            },
            {
                path: '/permission',
                name: 'permission',
                meta: {
                    title: '权限管理',
                    permiss: '13',
                },
                component: () => import(/* webpackChunkName: "permission" */ '../views/permission.vue'),
            },
            {
                path: '/upload',
                name: 'upload',
                meta: {
                    title: '上传插件',
                    permiss: '6',
                },
                component: () => import(/* webpackChunkName: "upload" */ '../views/upload.vue'),
            },
            {
                path: '/icon',
                name: 'icon',
                meta: {
                    title: '自定义图标',
                    permiss: '10',
                },
                component: () => import(/* webpackChunkName: "icon" */ '../views/icon.vue'),
            },
            {
                path: '/user',
                name: 'user',
                meta: {
                    title: '个人中心',
                },
                component: () => import(/* webpackChunkName: "user" */ '../views/user.vue'),
            },
            {
                path: '/editor',
                name: 'editor',
                meta: {
                    title: '富文本编辑器',
                    permiss: '8',
                },
                component: () => import(/* webpackChunkName: "editor" */ '../views/editor.vue'),
            },
            {
                path: '/markdown',
                name: 'markdown',
                meta: {
                    title: 'markdown编辑器',
                    permiss: '9',
                },
                component: () => import(/* webpackChunkName: "markdown" */ '../views/markdown.vue'),
            },
            {
                path: '/export',
                name: 'export',
                meta: {
                    title: '导出Excel',
                    permiss: '2',
                },
                component: () => import(/* webpackChunkName: "export" */ '../views/export.vue'),
            },
            {
                path: '/import',
                name: 'import',
                meta: {
                    title: '导入Excel',
                    permiss: '2',
                },
                component: () => import(/* webpackChunkName: "import" */ '../views/import.vue'),
            },
        ],
    },
    {
        path: '/login',
        name: 'Login',
        meta: {
            title: '登录',
        },
        component: () => import(/* webpackChunkName: "login" */ '../views/login.vue'),
    },
    {
        path: '/403',
        name: '403',
        meta: {
            title: '没有权限',
        },
        component: () => import(/* webpackChunkName: "403" */ '../views/403.vue'),
    },
];

const router = createRouter({
    history: createWebHashHistory(),
    routes,
});

router.beforeEach((to, from, next) => {
    document.title = `${to.meta.title} | vue-manage-system`;
    const role = localStorage.getItem('ms_username');
    const permiss = usePermissStore();
    if (!role && to.path !== '/login') {
        next('/login');
    } else if (to.meta.permiss && !permiss.key.includes(to.meta.permiss)) {
        // 如果没有权限，则进入403
        next('/403');
    } else {
        next();
    }
});

export default router;
